package consys.social.core.dao;

import java.text.ParseException;
import org.apache.log4j.Logger;
import consys.social.core.service.SocialUtils;
import consys.social.core.bo.DiscussionPost;
import consys.social.core.bo.Entity;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;
import static consys.social.nosql.service.hbase.HBaseHelpers.*;

/**
 * loads one discussion
 *
 * @author Petr Kunc
 */
public class DiscussionLoader {
    private Logger logger = Logger.getLogger(DiscussionLoader.class);

    public DiscussionPost load(Result r) {
        logger.info("Loading discussion Post from result");
        DiscussionPost dp = new DiscussionPost();
        dp.setAddedBy(new Entity(Bytes.toString(getColumnValue(r, "info", "owner"))));
        dp.setText(Bytes.toString(getColumnValue(r, "text", "")));
        try {
            dp.setDate(SocialUtils.getKeyDate(r.getRow()));
        } catch (ParseException ex) {
            //should never happen
            logger.error("Parse date error");
        }
        return dp;
    }
}
